import { DataSource } from 'typeorm';
import { Task } from '../../../entities/task.entity';
import { Project } from '../../../entities/project.entity';
import { User } from '../../../entities/user.entity';
import { TaskStatus, Priority as TaskPriority } from '../../../types/enums';

/**
 * 任务种子数据
 * 负责初始化示例任务
 */
export class TaskSeed {
  constructor(private dataSource: DataSource) {}

  /**
   * 运行任务种子
   */
  async run(): Promise<void> {
    console.log('开始初始化任务数据...');

    const taskRepository = this.dataSource.getRepository(Task);
    const projectRepository = this.dataSource.getRepository(Project);
    const userRepository = this.dataSource.getRepository(User);

    // 查找管理员用户
    const adminUser = await userRepository.findOneBy({ username: 'admin' });

    if (!adminUser) {
      console.log('未找到管理员用户，跳过任务初始化');
      return;
    }

    // 查找所有项目
    const projects = await projectRepository.find();

    if (projects.length === 0) {
      console.log('未找到项目，跳过任务初始化');
      return;
    }

    // 为每个项目创建示例任务
    for (const project of projects) {
      const exampleTasks = [
        {
          title: `[${project.name}] 需求分析与规划`,
          description: '进行需求收集、分析与功能规划',
          status: TaskStatus.IN_PROGRESS,
          priority: TaskPriority.HIGH,
          assigneeId: adminUser.id,
          createdById: adminUser.id,
          projectId: project.id,
          startDate: new Date(),
          dueDate: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000), // 一周后
        },
        {
          title: `[${project.name}] 用户界面设计`,
          description: '设计用户界面和交互流程',
          status: TaskStatus.TODO,
          priority: TaskPriority.MEDIUM,
          assigneeId: adminUser.id,
          createdById: adminUser.id,
          projectId: project.id,
          startDate: new Date(),
          dueDate: new Date(Date.now() + 10 * 24 * 60 * 60 * 1000), // 十天后
        },
        {
          title: `[${project.name}] 开发实施`,
          description: '根据需求和设计进行功能开发',
          status: TaskStatus.TODO,
          priority: TaskPriority.MEDIUM,
          assigneeId: adminUser.id,
          createdById: adminUser.id,
          projectId: project.id,
          startDate: new Date(Date.now() + 10 * 24 * 60 * 60 * 1000), // 十天后
          dueDate: new Date(Date.now() + 20 * 24 * 60 * 60 * 1000), // 二十天后
        },
        {
          title: `[${project.name}] 测试与验证`,
          description: '进行功能测试和验证',
          status: TaskStatus.TODO,
          priority: TaskPriority.MEDIUM,
          assigneeId: adminUser.id,
          createdById: adminUser.id,
          projectId: project.id,
          startDate: new Date(Date.now() + 20 * 24 * 60 * 60 * 1000), // 二十天后
          dueDate: new Date(Date.now() + 25 * 24 * 60 * 60 * 1000), // 二十五天后
        },
        {
          title: `[${project.name}] 部署上线`,
          description: '将完成的功能部署到生产环境',
          status: TaskStatus.TODO,
          priority: TaskPriority.HIGH,
          assigneeId: adminUser.id,
          createdById: adminUser.id,
          projectId: project.id,
          startDate: new Date(Date.now() + 25 * 24 * 60 * 60 * 1000), // 二十五天后
          dueDate: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000), // 三十天后
        },
      ];

      for (const taskData of exampleTasks) {
        const task = taskRepository.create(taskData);
        await taskRepository.save(task);
        console.log(`为项目 ${project.name} 创建任务: ${task.title}`);
      }
    }

    console.log('任务数据初始化完成');
  }
} 